let app =getApp();
let timer = ''
let sss = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count: 0,
    tim:5,
    huan: '欢迎回家'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options) {
      app.data.out=options.out;
      if (app.data.out == 'chu') {
        this.setData({
          huan: '期待重逢'
        })
      }
      wx.getSystemInfo({
        success: function (res) {
          that.data.width = res.windowWidth
          that.data.height = res.windowHeight
          var width = (that.data.width / 750) * 600;
          var height = (that.data.width / 750) * 600;
          that.times(width, height)
          sss = setInterval(function(){
            let tim = that.data.tim
            tim--
            that.setData({
              tim: tim
            })
            if (tim<1){
              if (app.data.out == 'chu') {
                const min = options.min
                const sec = options.sec
                wx.redirectTo({
                  url: '../conclusion/conclusion?min=' + min + '&sec=' + sec
                })
              } else if (app.data.out == 'jin') {
                wx.switchTab({
                  url: '../sports/sports'
                })
              }
              clearInterval(sss)
            }
          },1000)
        }
      })
    }
    setTimeout(function () {
      wx.vibrateLong()
    }, 100)
    setTimeout(function(){
      wx.vibrateLong()
    },900)
  },
  times(width, height){
    const _this = this
    let count = _this.data.count
    count+=2;
    _this.setData({
      count: count
    })
    if (count>100){
      clearTimeout(timer)
      return false
    }
    this.draws(width, height, count/100);
    timer = setTimeout(function () {
      _this.times(width, height)
    }, 100)
  },
  draws(width, height,speed){
    const cxt_arc = wx.createCanvasContext('canvasArc')
    cxt_arc.clearRect(0,0,width,height)
    cxt_arc.setStrokeStyle('#F4F4F4')
    cxt_arc.setLineCap('round')
    cxt_arc.translate(width / 2, height / 2)
    cxt_arc.setLineWidth(1)
    cxt_arc.rotate(-Math.PI / 2)//时间从3点开始，倒转90度 
    for (var i = 0; i < 60; i++) {
      cxt_arc.beginPath();
      cxt_arc.rotate(Math.PI / 30);
      cxt_arc.moveTo(width / 2 - 5, 0);
      cxt_arc.lineTo(width / 2 - 15, 0);
      cxt_arc.stroke();
    }
    // context.draw()
    cxt_arc.setLineWidth(10)
    cxt_arc.setStrokeStyle('#F5F5F5');
    cxt_arc.setLineCap('round');
    cxt_arc.beginPath();//开始一个新的路径  
    cxt_arc.arc(0, 0, width / 2 - 30, 0, 2 * Math.PI, false);//设置一个原点(106,106)，半径为100的圆的路径到当前路径  
    cxt_arc.stroke();//对当前路径进行描边  

    cxt_arc.setLineWidth(10);
    cxt_arc.setStrokeStyle('#F26640');
    cxt_arc.setLineCap('round');
    cxt_arc.beginPath();//开始一个新的路径  
    cxt_arc.arc(0, 0, width / 2 - 30, 0, 2 * Math.PI * speed, false);
    cxt_arc.stroke();//对当前路径进行描边  
    cxt_arc.draw();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    timer = ''
    clearTimeout(timer)
    clearInterval(sss)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    timer = ''
    clearTimeout(timer)
    clearInterval(sss)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})